package com.refid.api.cmd;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.refid.api.util.BuleUtil;

public class KLMCmd extends Cmd {
	private static final String CMD_SEND = "00";// 命令帧: 由上位机发送给40505W-M 芯片
	private static final String CMD_RST = "01";// 响应帧: 由40505W-M 芯片发回给上位机
	private static final String CMD_NOTICE = "02";// 通知帧: 由40505W-M 芯片发回给上位机
	public static final String CMD_START = "BB";// 命令开始
	public static final String CMD_END = "7E";// 命令结束
	public static final String NO_RST = CMD_START + " 01 FF 00 01 15 16 " + CMD_END;// 无响应

	@Override
	protected String repairCmd(String hexs) {
		hexs = BuleUtil.trimTONull(hexs);
		String checkSum = checkSum(hexs);
		String cmd = CMD_START + " " + CMD_SEND + " " + hexs + " " + checkSum + " " + CMD_END;
		return cmd;
	}

	@Override
	public HashSet<String> cmdTOEcp(List<String> list) {
		HashSet<String> set = new HashSet<String>();
		if (list == null || list.size() == 0)
			return set;
		String cmd = null;
		String cmdType = null;
		String cmdNm = null;
		for (int i = 0; i < list.size(); i++) {
			cmd = list.get(i);
			cmd = BuleUtil.trimTONull(cmd);
			if (cmd == null || cmd.length() < 3 * 8 + 4 * 3)
				continue;
			cmdType = cmd.substring(3, 5);
			cmdNm = cmd.substring(6, 8);
			if ((CMD_NOTICE.equals(cmdType) || CMD_RST.equals(cmdType))// && !NO_RST.equals(cmd)
					&& ("22".equals(cmdNm) || "27".equals(cmdNm))) {
				cmd = cmd.substring(3 * 8, cmd.length() - 3 * 4);
				if (cmd.length() == 35)
					set.add(cmd);
			}
		}
		return set;
	}

	@Override
	public List<String> cmds(String hexs) {
		List<String> rst = new ArrayList<String>();
		hexs = BuleUtil.trimTONull(hexs);
		if (hexs == null)
			return rst;
		Pattern r = Pattern.compile("BB.*?7E");
		Matcher m = r.matcher(hexs.toUpperCase());
		String cmdHex = null;
		while (m.find()) {
			cmdHex = m.group(0);
			cmdHex = BuleUtil.trimTONull(cmdHex);
			if (cmdHex != null) {
				rst.add(cmdHex);
			}
		}
		return rst;
	}

	@Override
	public String checkSum(String hexs) {
		if (BuleUtil.trimTONull(hexs) == null)
			return "";
		int cnt = 0;
		String hex = null;
		String[] args = hexs.split(" ");
		for (int i = 0; i < args.length; i++) {
			hex = args[i];
			hex = BuleUtil.trimTONull(hex);
			if (hex == null)
				continue;
			cnt = cnt + Integer.parseInt(hex, 16);
		}
		hex = byteToHex((byte) cnt);
		return hex;
	}

	public static void main(String[] args) {
		Cmd cmd = new KLMCmd();
		System.out.println(cmd.repairCmd("07 00 01 01"));
		System.out.println(cmd.repairCmd("27 00 03 22 00 04"));// BB 00 27 00 03 22 00 04 50 7E
		System.out.println(cmd.repairCmd("27 00 03 22 00 02"));// BB 00 27 00 03 22 00 02 4E 7E
		System.out.println("========");
		String msg = "bb 00 39 00 09 00 00 00 00 00 00 00 00 04 46 7E bb 00 39 00 09 00 00 00 00 01 00 00 00 08 4B 7E bb 00 39 00 09 00 00 00 00 02 00 00 00 06 4a 7E bb 00 39 00 09 00 00 00 00 03 00 00 00 04 49 7E bb 00 0C 00 13 01 00 00 00 20 60 00 11 11 22 22 33 33 44 44 55 55 A6 1B 5F 7E bb 00 39 00 09 00 00 00 00 00 00 00 00 08 4A 7E bb 00 82 00 07 12 34 56 78 02 00 00 9F 7E";
		List<String> list = cmd.cmds(msg);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

	}
}
